package com.liangdemo.web.thread;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

/**
 * TransferQueue队列
 * TransferQueue容量为0
 * 实现线程同步
 */
public class TransferQueueTest {
    public static void main(String[] args) {
        String str1 = "1234567890";
        String Str2 = "ABCDEFGHRJK";
        String[] array1 = str1.split("");
        String[] array2 = Str2.split("");
        //创建一个队列
        TransferQueue<String> queue = new LinkedTransferQueue<>();
        new Thread(() -> {

            try {
                for (String a : array1) {
                    System.out.print(queue.take());//获取一个字符  .如果没有获取字符，会导致线程停止
                    queue.transfer(a);//放入一个字符
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, "线程1").start();
        new Thread(() -> {
            try {
                for (String s : array2) {
                    queue.transfer(s);
                    System.out.print(queue.take());
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, "线程2").start();
    }
}
